<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@taglib prefix="s" uri="/struts-tags"  %>
<%
	String operation = request.getParameter("operation");
%>
<%@ page import="com.starbaba.core.web.constant.WebConstants"%>
<%@ page import="com.starbaba.system.user.model.UserVO"%>
<!DOCTYPE html>
<html lang="en">
<head>
<%@ include file="/common/common-css.jsp"%>
<style>
.table>th {
	vertical-align: center;
	background-color: grey;
}
input[type="radio"] {
   font-size: 100px;
   vertical-align:middle;
   margin-top:-2px; 
   margin-left:2px;
   margin-bottom:1px;
}
</style>
</head>
<body class="no-skin">
	<div class="page-content">
		<div class="page-content-area">
			<div class="row">
				<div class="col-xs-12 col-sm-12 widget-container-col">
					<div class="widget-box widget-color-blue">
						<!-- #section:custom/widget-box.options.collapsed -->
						<div class="widget-header widget-header-small">
							<h6 class="widget-title lighter">
								<i class="ace-icon glyphicon glyphicon-th-list"></i>一级项名
							</h6>
                            <div class="widget-toolbar no-border">
								<a href="#" data-action="collapse">
									<i class="ace-icon fa fa-chevron-up"></i>
								</a>
							</div>
						</div>
						
						<div class="widget-body">
							<div class="widget-main">
								<form class="form-horizontal" id="queryForm" method="post" enctype="multipart/form-data">
									<input type="hidden" name="index" id="index" value="${paramsMap.ruleCount}"> 
									<input type="hidden" name="itemId" id="itemId" value="${paramsMap.itemId}"> 
									
									<table id="baseinfo" class="table table-bordered table-striped table-hover table-condensed table-responsive table">
										<tbody id="valiTable">
	                                        <tr header="header">
												<th width="80px">校验方式</th>
												<th width="120px">校验规则</th>
												<th width="80px">错误提示信息</th>
												<th width="80px">操作</th>
											</tr>
											<c:forEach items="${paramsMap.ruleList}" var="rule" varStatus="status">
											<tr id="valiRule${status.index+1}">
												<td>
													<input type="hidden" name="valiRuleId${status.index+1}" id="valiRuleId${status.index+1}" value="${rule.id}">
													<select id="validateId${status.index+1}" name="validateId${status.index+1}">
														<option value="">请选择</option>
														<c:forEach items="${paramsMap.valiList}" var="vali">
															<c:choose>
																<c:when test="${rule.validate_id eq vali.id}">
																	<option value="${vali.id}" selected="selected">${vali.rule_name}</option>
																</c:when>
																<c:otherwise>
																	<option value="${vali.id}">${vali.rule_name}</option>
																</c:otherwise>
															</c:choose>
														</c:forEach>
													</select>
												</td>
												<td>
													<input type="text" id="params${status.index+1}" name="params${status.index+1}"
														value="${rule.params}" class="col-xs-12 col-sm-4" style="width:150px;">
												</td>
												<td>
													<input type="text" id="errorMsg${status.index+1}" name="errorMsg${status.index+1}"
														value="${rule.errorMsg}" class="col-xs-12 col-sm-4" style="width:150px;">
												</td>
												<td>
													<button type="button" class="btn btn-primary btn-sm no-border" onclick="deleteRule(${status.index+1});">删除</button>
												</td>
											</tr>
											</c:forEach>
										</tbody>
									</table>
								</form>
									
							</div><!-- /.widget-main -->
							<div style="margin: 10px;">
								<button type="button" class="btn btn-primary btn-sm no-border" onclick="addValiRule();">
									<i class="ace-icon glyphicon glyphicon-plus bigger-110"></i>新增规则
								</button>
							</div>	
						</div><!-- /.widget-body -->
						
					</div><!-- /.widget-box -->
				</div><!-- /.col-sm-12 -->
			</div>
			
			<div class="clearfix form-actions">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn  btn-inverse  no-border " onclick="doCancle()"
						id="delete">
						<i class="ace-icon fa fa-undo bigger-160"></i> 取消
					</button>
					&nbsp; &nbsp; &nbsp;
					<button id="btn-save" name="btn-save"
						class="btn  btn-success  no-border" type="button"
						onclick="doSave()">
						<i class="ace-icon fa fa-floppy-o bigger-160"></i> 保存
					</button>
				</div>
			</div>
		</div>
	</div>

	<%@ include file="/common/common-js.jsp"%>
	<%@ include file="/discovery/maxlength.jsp"%>
	<script type="text/javascript" src="${ctx}/ace/js/maxlength.js"></script>
	<script type="text/javascript" src="${ctx}/js/wangEditor-1.3.11.min.js"></script>
	<script type="text/javascript" src="${ctx}/js/jquery.form.js"></script>
	<script type="text/javascript" src="${ctx}/js/call_func.js"></script>
	<script>
		var $multiple = 1;
		var $selector = '';
		var $input = '';
		
		var jsonVali = JSON.parse('${paramsMap.jsonVali}');

		$(function() {
			$('#img').change(function() {
				var file = this.files[0];
				var r = new FileReader();
				r.readAsDataURL(file);
				$(r).load(function() {
					$('#thumbnail').attr('src', this.result);
					$('#img').attr("value", this.result);
				});
			});

			//点击按钮
			$('.get-img-modal').on('click', function() {
				//展示模态框
				var $this = $(this);
				var $title = $this.attr('data-title');
				$input = $this.attr('data-input');
				$selector = $this.attr('data-selector');
				$multiple = $this.attr('data-multiple');
				if ($multiple == 1) {
					$('input[name="myFile"]').prop('multiple', true);
				} else {
					$('input[name="myFile"]').prop('multiple', false);
				}
				$('#Modal').find('h4.modal-title').html($title);
				$('#Modal').modal('show');
				return false;
			});

			$('.thumbnail').on('click', function() {
				$('#thumbnail').attr('src', "");
				$("input[id=thumbnail]").val("");
			});

		});
		
		//获取项名的下拉框
		function getValiListHtml(index){
			var valiHtml = '';
			valiHtml += '<select id="validateId'+index+'" name="validateId'+index+'">';
			valiHtml += 	'<option value="">请选择</option>';
			for(var i=0;i<jsonVali.length;i++){
				valiHtml += '<option value="'+jsonVali[i].id+'">'+jsonVali[i].rule_name+'</option>';
			}
			valiHtml += '</select>';
			return valiHtml;
		}
		
		function addValiRule(){
			var valiTable = $("#valiTable");
			var index = $("#index").val();
			index++;
			var html = "";
			html += '<tr id="valiRule'+index+'">'
			     + 		'<td>'
			     + 			getValiListHtml(index)
			     + 		'</td>'
			     + 		'<td>'
			     + 			'<input type="text" id="params"'+index+' name="params'+index+'" class="col-xs-12 col-sm-4" style="width:150px;">'
			     + 		'</td>'
			     + 		'<td>'
			     + 			'<input type="text" id="errorMsg'+index+'" name="errorMsg'+index+'" class="col-xs-12 col-sm-4" style="width:150px;">'
			     + 		'</td>'
			     + 		'<td>'
			     + 			'<button type="button" class="btn btn-primary btn-sm no-border" onclick="deleteVali('+index+');">删除</button>'
			     + 		'</td>'
			     + 	'</tr>';
			
			$("#index").val(index);
			valiTable.append(html);
		}

		function deleteRule(index){
			var valiRuleTab = $("#valiRule"+index);
			var valiRuleId = $("#valiRuleId"+index).val();
			var answer = window.confirm("是否确认删除?");
			if(answer){
				if(isBlock(valiRuleId)){  //临时添加的option
					valiRuleTab.remove();
				}else{  //先删除数据库数据，再删除页面option
					var data = {"valiRuleId":valiRuleId};
					$.ajax({
						type:"post",	
						async:true,
						data:data,
						url: "${ctx}/apiActivityConf/deleteValiRule.action",
						success:function(resp){
							valiRuleTab.remove();
						},
						error:function(error){
							alert("删除失败！");
						}
					});
				}
			}
		}

		function doCancle() {
			try {
				if (parent) {
					parent.parent['__extdialog_active'].dialog('close');
				}
			} catch (e) {
				window.history.back();
			}
		}

		function doSave() {
			var flag = false; 
			var validateIdTab = $("select[id^='validateId']");
			validateIdTab.each(function(index,el){
				var $this = $(el);
				var val = $this.val();
				if(isBlock(val)){
					flag = true;
					return;
				}
			});
			if(flag){
				parent.bootbox.alert("请选择校验方式");
				return;
			}
			
			saveForm();
		}

		function saveForm() {
			var saveBtn = $("#btn-save");
			saveBtn.attr("disabled","disabled");
			
			var saveUrl = "${ctx}/apiActivityConf/saveValiRule.action"; 
			var options = {
				url : saveUrl,
				type : "POST",
				dataType : "json",
				beforeSend : function() {
					$(".load_bg,.load_img").show();
				},
				success : function(data) {
					saveBtn.removeAttr("disabled");
					
					parent.parent['__extdialog_active'].dialog('close');
					var jsonObj = JSON.parse(data);
					var success = jsonObj.success;
					if(success == '1'){
						parent.bootbox.alert("保存成功");
					}else{
						parent.bootbox.alert("保存失败");
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					saveBtn.removeAttr("disabled");
					alert(textStatus);
					console.log(XMLHttpRequest, textStatus, errorThrown);
				},
				complete : function() {
					saveBtn.removeAttr("disabled");
					$(".load_bg,.load_img").hide();
				}
			};
			$("#queryForm").ajaxSubmit(options);
		}
		
		function isBlock(value){
			if(value==""||value==null||value==undefined){
				return true;
			}
			return false;
		}
	</script>
</body>
</html>
